//
//  FQDMyCommentCell.m
//  FakeQDaily
//
//  Created by mewxiv on 9/28/16.
//  Copyright © 2016 Xensius. All rights reserved.
//

#import "FQDMyCommentCell.h"
#import "FQDRestModel.h"
#import "NSDate+FQDExtension.h"
#import "FQDMyCommentMessageCellItem.h"

#import "Masonry.h"
#import "UIImageView+WebCache.h"

static CGFloat const kAvatarImageSize = 35.0f;

@interface FQDMyCommentCell ()

@property (nonatomic, strong) UILabel *commentContentLabel;
@property (nonatomic, strong) UILabel *dateLabel;
@property (nonatomic, strong) UIView *separatorLine;
@property (nonatomic, strong) UIImageView *avatarImageView;
@property (nonatomic, strong) UILabel *authorNameLabel;
@property (nonatomic, strong) UILabel *articleContentLabel;

@end

@implementation FQDMyCommentCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self prepareCell];
        [self prepareConstraints];
    }
    return self;
}

- (void)prepareCell
{
    _commentContentLabel = [[UILabel alloc] init];
    _commentContentLabel.font = FQDRegularFontWithSize(13.0f);
    _commentContentLabel.textAlignment = NSTextAlignmentLeft;
//    _commentContentLabel.numberOfLines = 0;
    [self.contentView addSubview:_commentContentLabel];
    
    _dateLabel = [[UILabel alloc] init];
    _dateLabel.font = FQDRegularFontWithSize(12.0f);
    _dateLabel.textColor = RGB(179, 179, 179);
    _dateLabel.textAlignment = NSTextAlignmentRight;
    [self.contentView addSubview:_dateLabel];
    
    _separatorLine = [[UIView alloc] init];
    _separatorLine.backgroundColor = RGB(237, 237, 237);
    [self.contentView addSubview:_separatorLine];
    
    UIImage *defaultAvatarImage = [UIImage imageNamed:@"avatar_default_icon_normal"];
    _avatarImageView = [[UIImageView alloc] initWithImage:defaultAvatarImage];
    _avatarImageView.layer.cornerRadius = kAvatarImageSize / 2;
    _avatarImageView.clipsToBounds = YES;
    [self.contentView addSubview:_avatarImageView];
    
    _authorNameLabel = [[UILabel alloc] init];
    _authorNameLabel.font = FQDRegularFontWithSize(14.0f);
    _authorNameLabel.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:_authorNameLabel];
    
    _articleContentLabel = [[UILabel alloc] init];
    _articleContentLabel.font = FQDRegularFontWithSize(12.0f);
    _articleContentLabel.textColor = RGB(179, 179, 179);
    _articleContentLabel.textAlignment = NSTextAlignmentLeft;
    _articleContentLabel.numberOfLines = 0;
    [self.contentView addSubview:_articleContentLabel];
}

- (void)prepareConstraints
{
    WeakSelf;
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(weakSelf);
    }];
    
    [_commentContentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.and.left.equalTo(weakSelf.contentView).offset(15);
        make.right.equalTo(weakSelf.dateLabel.mas_left).offset(-10);
    }];
    
    [_dateLabel setContentHuggingPriority:UILayoutPriorityDefaultLow+1 forAxis:UILayoutConstraintAxisHorizontal];
    [_dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.contentView).offset(15);
        make.right.equalTo(weakSelf.contentView).offset(-15);
    }];
    
    [_separatorLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.commentContentLabel.mas_bottom).offset(10);
        make.left.equalTo(weakSelf.contentView).offset(15);
        make.right.equalTo(weakSelf.contentView).offset(-15);
        make.height.equalTo(@0.5);
    }];
    
    [_avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.separatorLine.mas_bottom).offset(10);
        make.left.equalTo(weakSelf.contentView).offset(15);
        make.size.equalTo(@(kAvatarImageSize));
    }];

    [_authorNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.separatorLine.mas_bottom).offset(10);
        make.left.equalTo(weakSelf.avatarImageView.mas_right).offset(10);
        make.right.equalTo(weakSelf.contentView.mas_right);
    }];
    
    [_articleContentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.authorNameLabel.mas_bottom).offset(5);
        make.left.equalTo(weakSelf.avatarImageView.mas_right).offset(10);
        make.right.equalTo(weakSelf.contentView.mas_right).offset(-15);
        make.bottom.equalTo(weakSelf.contentView).offset(-10);
    }];
}

- (void)configureCellWithCellItem:(FQDMyCommentMessageCellItem *)cellItem
{
    [super configureCellWithCellItem:cellItem];
    
    FQDMyCommentMessage *message = cellItem.commentMessage;
    
    NSString *dateString = [[NSDate dateWithTimeIntervalSince1970:message.publish_time] dateStringSinceNow];
    NSURL *avatarURL = [NSURL URLWithString:message.target_userface];
    UIImage *defaultAvatarImage = [UIImage imageNamed:@"avatar_default_icon_normal"];
    
    self.commentContentLabel.text = message.content;
    self.dateLabel.text = dateString;
    self.authorNameLabel.text = message.target_username;
    self.articleContentLabel.text = message.target_content;
    [self.avatarImageView sd_setImageWithURL:avatarURL placeholderImage:defaultAvatarImage];
}

+ (NSString *)reuseId
{
    return @"FQDMyCommentCell";
}

@end
